Skip to content

Instantly share code, notes, and snippets.

@cehteh
Last active November 11, 2022 02:16
Show Gist options
  • Save cehteh/3c44f46f088f7723816ce98f8af53362 to your computer and use it in GitHub Desktop.
Save cehteh/3c44f46f088f7723816ce98f8af53362 to your computer and use it in GitHub Desktop.
quadrature decoding brainstorming
uint8_t encoder_state;
int32_t position;
encoder ()
{
encoder_state = encoder_state<<2 | PB1<<1 | PB2;
switch (encoder_state & 0x0f)
{
// one direction
case 0b0001:
case 0b0111:
case 0b1110:
// break; .. when only full cycles shall be counted
case 0b1000:
++position;
break;
// other direction
case 0b0010:
case 0b1011:
case 0b1101:
// break;
case 0b0100:
--position;
break;
// no transition (when polling and not interrupt driven)
case 0b0000:
case 0b0101:
case 0b1010:
case 0b1111:
// maybe: encoder_state >>= 2; // drop this transition
break;
// the errorneous transitions
//case 0b0110:
//case 0b1001:
//case 0b1100:
//case 0b0011:
default:
/* handle error */
//note that the higher bits (encoder_state & 0xfc) contain history of previous 3 moves
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment